﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.Model.View;
using System.Data;

namespace HZ.Supervise.Web.Secondphase.Statistics
{
    public partial class HospitalPurchasePlan : BasePage
    {
         
        IExecuteContext iec = HZ.Supervise.BLLFactory.ExecuteContext();
        IBLL_Sort isort = HZ.Supervise.BLLFactory.IBLL_Sort();
        public HospitalPurchasePlan()
        {

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                txtPlanTime.Text = Convert.ToString(System.DateTime.Now.Year.ToString() + "-" + (System.DateTime.Now.Month - 1).ToString() + "-1");
                BindData();
                rblSortBind();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_CompanyPS_PageChanged(object sender, EventArgs e)
        { 
            BindData();

        }
        /// <summary>
        /// 绑定药品类别
        /// </summary>
        private void rblSortBind()
        {
            //绑定中标目录类型
            DataTable dt = isort.GetSort();
            dlpsort.DataSource = dt;
            dlpsort.DataValueField = "SortID";
            dlpsort.DataTextField = "SortName";
            dlpsort.DataBind();
           
            this.dlpsort.Items.Insert(0, new ListItem("全部", ""));
        }
        /// <summary>
        /// 绑定数据
        /// modify  by张雄  2011.5.4
        /// </summary>
        public void BindData()
        {
            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            int t = 0;
            System.Data.DataTable dt = iec.GetHospitalPurchasePlan(vs, this.AspNetPager_CompanyPS.CurrentPageIndex, AspNetPager_CompanyPS.PageSize, out t);
            AspNetPager_CompanyPS.RecordCount = t;

            this.gvwPSLInfo.PageSize = AspNetPager_CompanyPS.PageSize;
            this.gvwPSLInfo.DataSource = dt;
            this.gvwPSLInfo.DataBind();



        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager_CompanyPS.CurrentPageIndex = 1;
        }
        



        /// <summary>
        /// 导出 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            int t = 0;
            System.Data.DataTable dt = iec.GetHospitalPurchasePlan(vs, 1, 0, out t);
            
            ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
        }



        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private SuperviseQuery Queryby()
        {
            SuperviseQuery vs = new SuperviseQuery();
            
            if (!string.IsNullOrEmpty(txtPlanTime.Text .Trim ()))
            {
                vs.PlanTimeFrom = Convert.ToDateTime(txtPlanTime.Text.Trim());
            }
            //vs.HospitalName = txtHospitalName.Text.Trim();
            vs.AreaID = HZ.Supervise.Web.Function.GetAreaIDBySQL(AreaControl1.SelectedAreaID);
            vs.ProcureCatalogID = txtProcureCatalogID.Text.Trim();
           // vs.SortID = dlpsort.SelectedValue;
            vs.sid = dlpsort.SelectedValue;
            if (HZ.Common.zhHansUtil.HaveChineseCode(txtProductName.Text.Trim()))
            {
                vs.ProductName = txtProductName.Text.Trim();
            }
            else
            {
                vs.PinyinCode = txtProductName.Text.Trim();
            }
            vs.HospitalName = txtHospitalName.Text.Trim();
            vs.MedicineModelName = txtMedicineModel.Text.Trim();
            vs.Pack = txtPack.Text.Trim();
            switch (ddlHosType.SelectedValue)
            {
                case "0":
                    break;
                case "1":
                    vs.Levels = "'3','4','5','6'";
                    break;
                case "2":
                    vs.IsCountry = "'1','2','3','4'";
                    break;
                case "3":
                    vs.IsCountry = "'2'";
                    break;
                case "4":
                    vs.IsCountry = "'1'";
                    break;
                default:
                    break;
            }

            vs.IsGovernment = ddlIsGov.SelectedValue; 
            vs.SortFields = ddlSortFields.SelectedValue + " " + ddlSort.SelectedValue;
            return vs;
        }
        protected void gvwPSLInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            string sessionId = Guid.NewGuid().ToString();
            Control cmdControl = e.CommandSource as Control;
            GridViewRow row = cmdControl.NamingContainer as GridViewRow;
 
            vs.ProductName = row .Cells[2].Text ;
            vs.MedicineModelName = row .Cells[3].Text ;
            vs.Pack = row .Cells[4].Text ;
            
            vs.CompanyName_SC = row .Cells[6].Text ;
            if (e.CommandName == "lbnCom")
            {
                LinkButton lbnR = (LinkButton)row.FindControl("lbnCom");
                vs.ProcureCatalogID = lbnR.CommandArgument;
                Session[sessionId] = vs;
                Response.Redirect("HospitalPurchasePlanByHospital.aspx?sesid=" + sessionId);
            }
        }
    }

}

